/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.pc.ui.client.actions;

import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JDialog;

import org.opu.pc.services.PublicationService;
import org.opu.pc.services.beans.PublicationBean;
import org.opu.pc.ui.client.widgets.PropertiesEditPane;
import org.opu.pc.ui.client.widgets.PropertiesEditPane.PropertyOption;

/**
 * 
 * @author yura
 */
public class PublicationSearch extends AbstractDialogAction {
    public static final String PUBLICATION_APPLIED = "applied";
    public static final String PUBLICATION_CONFERENCE_VENUE = "conference.venue";
    public static final String PUBLICATION_NAME = "name";
    public static final String PUBLICATION_PAGE_NUMBER_START = "page.start.number";
    public static final String PUBLICATION_PAGE_NUMBER_END = "page.end.number";
    public static final String PUBLICATION_PUBLICATIONPLACE = "publication.place";

	private static final long serialVersionUID = -3945327499830607941L;

	protected PublicationService publicationService;

	public void setPublicationService(PublicationService publicationService) {
		this.publicationService = publicationService;
	}

	@Override
	public String getName() {
		return "PublicationSearch";
	}

	@Override
	protected void initPropMap(Map<String, List<PropertyOption>> map) {
		map.put(PUBLICATION_APPLIED, Arrays.asList(
				new PropertyOption(PUBLICATION_APPLIED + ".null", "null", "null"),
				new PropertyOption(PUBLICATION_APPLIED + ".true", "true", "true"),
				new PropertyOption(PUBLICATION_APPLIED + ".false", "false", "false")));
	}

	@Override
	protected void initProp(LinkedHashMap<String, Object> properties) {
		properties.put(PUBLICATION_NAME,"");
		properties.put(PUBLICATION_PUBLICATIONPLACE,"");
		appendDate(properties);
		properties.put(PUBLICATION_CONFERENCE_VENUE,"");
		properties.put(PUBLICATION_PAGE_NUMBER_START, 0);
		properties.put(PUBLICATION_PAGE_NUMBER_END, 0);
		properties.put(PUBLICATION_APPLIED,"");
	}

    protected void appendDate(LinkedHashMap<String, Object> properties) {
        properties.put("publication.start.date", (Date)null);
        properties.put("publication.end.date", (Date)null);
    }

	@Override
	protected OkAction getOkAction(JDialog dialog, final PropertiesEditPane pane) {
		return new MOkAction(pane, dialog);
	}

	protected Boolean getAppliedBool(Map<String, Object> prop) {
		String bool = (String)prop.get(PUBLICATION_APPLIED);
		return "null".equals(bool) ? null : Boolean.parseBoolean(bool);
	}

	protected String getName(Map<String, Object> prop) {
		return (String)prop.get(PUBLICATION_NAME);
	}

	private final class MOkAction extends OkAction implements SerialAction {
		String sName = "";
		Boolean wApl;
		private static final long serialVersionUID = -6845124793534976057L;

		public MOkAction(PropertiesEditPane pane, JDialog dialog) {
			super(pane, dialog);
		}

		@Override
		public void runAction(ActionEvent e) throws Exception {
			Map<String, Object> prop = pane.getProperties();
			sName = PublicationSearch.this.getName(prop);
			wApl = getAppliedBool(prop);
			runAgaion();
		}

		@Override
		public void runAgaion() {
			hideDialog();
			logger.debug("Start search publication [" + sName + "; " + wApl
					+ "]");
			mainView.setContent(publicationService.searchByName(sName, wApl),
					PublicationBean.class);
		}
	}
}
